<template>
  <BasicDrawer
    v-bind="$attrs"
    @register="registerDrawer"
    showFooter
    :title="getTitle"
    width="90%"
    @ok="handleSubmit"
    :showOkBtn="false"
    :destroyOnClose="true"
  >
    <FormManagementEdit ref="formManagementEditRef" @success="handleSubmit" />
  </BasicDrawer>
</template>
<script lang="ts">
  import { defineComponent, ref, computed, unref } from 'vue';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import FormManagementEdit from './FormManagementEdit.vue';
  export default defineComponent({
    name: 'FormManagementDrawer',
    components: {
      BasicDrawer,
      FormManagementEdit,
    },
    emits: ['success', 'register'],
    setup(_, { emit }) {
      const isUpdate = ref(true);
      const formManagementEditRef = ref(null);
      const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
        setDrawerProps({ confirmLoading: false });
        isUpdate.value = data?.isUpdate;
        console.log('data', data);
        try {
          formManagementEditRef.value?.fetch(data);
        } catch (error) {
          console.log(error);
        } finally {
        }
      });

      const getTitle = computed(() => (!unref(isUpdate) ? '新增' : '编辑'));

      async function handleSubmit() {
        try {
          if (!unref(isUpdate)) {
            closeDrawer();
          }
          emit('success');
        } finally {
          setDrawerProps({ confirmLoading: false });
        }
      }

      return {
        registerDrawer,
        getTitle,
        handleSubmit,
        formManagementEditRef,
      };
    },
  });
</script>
